package cn.change.org.service.impl;

import cn.change.basic.exception.BusinssException;
import cn.change.basic.service.impl.BaseServiceImpl;
import cn.change.basic.util.BaiduAuditUtils;
import cn.change.basic.vo.ShopVo;
import cn.change.org.domain.Employee;
import cn.change.org.domain.Shop;
import cn.change.org.domain.ShopAuditLog;
import cn.change.org.mapper.EmployeeMapper;
import cn.change.org.mapper.ShopAuditLogMapper;
import cn.change.org.mapper.ShopMapper;
import cn.change.org.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import sun.rmi.runtime.Log;

import java.util.List;

/**
 * @author 16252
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements ShopService {
    @Autowired
    ShopMapper shopMapper;

    @Autowired
    EmployeeMapper employeeMapper;

    @Autowired
    ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    JavaMailSender javaMailSender;

    /**
     * 1店铺是否被注册
     */
    @Override
    @Transactional
    public void settlement(Shop shop) {
        String name = shop.getName();
       /* !BaiduAuditUtils.TextCensor(shop.getName())*/
        //使用StringUtils工具类来判断是否为空
        if (StringUtils.isEmpty(name)){
            throw new BusinssException("店铺名字不能为空！！");
        }
        //百度ai智能审核
        if (!BaiduAuditUtils.TextCensor(name)){
            throw new BusinssException("改名字不合法！！");
        }
        Shop shopName = shopMapper.loadByName(name);
        if (shopName != null){
            throw new BusinssException("该店铺已被注册！！");
        }
        /** 2管理员是否被注册，是否绑定了店铺
         *  2.1注册，绑定了店铺
         *  2.2注册，未绑定店铺
         *  2.3未注册，也未绑定店铺
         */
        /* 2.1注册，也店铺*/
        Employee admin = shop.getAdmin();
        Employee shopOwner = employeeMapper.loadByUserName(admin.getUsername());
        if (shopOwner != null && shopOwner.getShop_id() != null ){
            throw new BusinssException("您已绑定了店铺，请勿重复绑定！！");
        }

        /*2.2未注册，也未绑定店铺*/
        if (shopOwner == null){
            employeeMapper.save(admin);
            shopOwner = admin;
        }
        //2.保存店铺（设置店铺管理员并保存店铺）
        shop.setAdmin_id(shopOwner.getId());
        shopMapper.save(shop);//返回自增Id
        //3.修改员工shopId
        shopOwner.setShop_id(shop.getId());
        employeeMapper.update(shopOwner);

    }

    /**
     * 通过
     * @param log
     */
    @Override
    public void pass(ShopAuditLog log) {
        Shop shop = new Shop();
        shop.getId();
        //修改为待激活
        if (log.getState() == 1){
            shop.setState(2);
            shopMapper.update(shop);
        }
        //日志
        log.setAudit_id(333);
        shopAuditLogMapper.save(log);
        //发邮件
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //设置发送人
        mailMessage.setFrom("1625259905@qq.com");
        //邮件主题
        mailMessage.setSubject("好消息，好消息！！");
        //邮件内容
        mailMessage.setText("您的店铺审核通过，请激活。链接：https://www.bilibili.com/video/BV1sT4y1i7AD?spm_id_from=333.851.b_7265636f6d6d656e64.2"+log.getShop_id());
        //收件人
        mailMessage.setTo("1625259905@qq.com");
        javaMailSender.send(mailMessage);
    }

    /**
     * 驳回
     * @param log
     */
    @Override
    public void reject(ShopAuditLog log) {
        //修改为驳回
        if (log.getState() == 0){
            Shop shop = new Shop();
            shop.setState(-1);
            shopMapper.update(shop);
        }
        //日志
        log.setAudit_id(333);
        shopAuditLogMapper.save(log);
        //发邮件
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //设置发送人
        mailMessage.setFrom("1625259905@qq.com");
        //邮件主题
        mailMessage.setSubject("驳回！");
        //邮件内容
        mailMessage.setText("您的店铺创建存在违规字符，请重试！"+ "链接：https://www.bilibili.com/video/BV1bS4y1N7yn/?spm_id_from=333.788.recommend_more_video.0"+log.getShop_id());
        //收件人
        mailMessage.setTo("1625259905@qq.com");
        javaMailSender.send(mailMessage);
    }


    @Override
    public List<ShopVo> loadStateStatistics() {
        return shopMapper.loadStateStatistics();
    }
}


